﻿using ConfExpert.Forms.Base;
using ConfExpert.Planlama;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Satinalma
{
    public partial class SipNo : XForm
    {
        public enum Yetki
        {
            YeniSipGirebilme = 1,
            yetki2 = 2,
            yetki3 = 3
        }
        
        public SipNo()
        {
            InitializeComponent();
        }

        //int auth = 0;
        BindingSource bsSipNo;
        private void SipNo_Load(object sender, EventArgs e)
        {
            bsSipNo = new BindingSource(ds, "xSipNo");

            InitGridView(gvSipNo, bsSipNo);

            GetDetails("xSipNo", "GetSipNoList", 0);
            //gvSipNo.MoveFirst();
            //gvSipNo.TopRowIndex = gvSipNo.FocusedRowHandle;
            CenterGridColumn(gvSipNo.Columns["Tip"]);

             
            btnYeniSip.Visible = true;
            /*
             1: KST formu
             * 
             * 
             * 
             * 
             0: KST sipariş satırları update+insert
             1: yeni sipariş no. almak
             2: özet sayfasında sadece kendinin değil herkesin siparişini görmek
             */




            gvSipNo.BestFitColumns();
            btnGuncelleSip.Click += new EventHandler(btnGuncelleSip_Click);
            btnKaydetSip.Click += new EventHandler(btnKaydetSip_Click);
            btnYeniSip.Click += new EventHandler(btnYeniSip_Click);
            cbtnAllowChange.CheckedChanged += new EventHandler(cbtnAllowChange_CheckedChanged);

            gvSipNo.DoubleClick += new EventHandler(gvSipNo_DoubleClick);
            gvSipNo.OptionsBehavior.Editable = false;

            gvSipNo.RowStyle += new RowStyleEventHandler(gvSipNo_RowStyle);
            this.Shown += new EventHandler(SipNo_Shown);
        }

        void SipNo_Shown(object sender, EventArgs e)
        {
            //gvSipNo.MoveLast();
            //gvSipNo.MakeRowVisible(gvSipNo.FocusedRowHandle, true);
        }

        void gvSipNo_RowStyle(object sender, RowStyleEventArgs e)
        {
            if (e.RowHandle == NewRow)
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }

        int NewRow = -1;
        void btnYeniSip_Click(object sender, EventArgs e)
        {
            using (YeniSiparis yeniSiparis = new YeniSiparis())
            {
                if (yeniSiparis.ShowDialog() == DialogResult.OK)
                {
                    GetDetails("xSipNo", "GetSipNoList", 0);
                    NewRow = gvSipNo.LocateByValue(0, gvSipNo.Columns["SipNo"], yeniSiparis.SipNo);
                    gvSipNo.TopRowIndex = NewRow;
                    //gvSipNo.MakeRowVisible(NewRow, true);
                    //gvSipNo.MoveLast();
                    //gvSipNo.MakeRowVisible(gvSipNo.FocusedRowHandle, true);
                    //gvSipNo.TopRowIndex = gvSipNo.FocusedRowHandle;
                }
            }
        }

        private void btnKaydetSip_Click(object sender, EventArgs e)
        {
            ExecuteNonQuery("UpdSipNoList", ds.xSipNo.SerializeChanges());
            ds.AcceptChanges();
            cbtnAllowChange.Checked = false;
        }

        void cbtnAllowChange_CheckedChanged(object sender, EventArgs e)
        {
            btnKaydetSip.Enabled = gvSipNo.OptionsBehavior.Editable = cbtnAllowChange.Checked;
            //TODO: editable = true olması yetmiyor; bütün sütunların AllowEdit property leri de setlenmeli!
        }

        void btnGuncelleSip_Click(object sender, EventArgs e)
        {
            string Current = string.Empty;
            if (bsSipNo.Current != null)
                Current = ((DataRowView)bsSipNo.Current).Row["SipNo"].ToString();

            ds.SipNo.Clear();
            GetDetails("xSipNo", "GetSipNoList", 0);
            Notify("SipNo refreshed.");

            if (Current != string.Empty)
                gvSipNo.TopRowIndex = gvSipNo.LocateByValue(0, gvSipNo.Columns["SipNo"], Current);

                //gvSipNo.MakeRowVisible(gvSipNo.LocateByValue(0, gvSipNo.Columns["SipNo"], Current), true);
        }

        public int SipAcanMarkaId = 0;
        public string PONr = string.Empty;
        void gvSipNo_DoubleClick(object sender, EventArgs e)
        {
            GridView view = ((GridView)sender);
            GridHitInfo hi = view.CalcHitInfo(view.GridControl.PointToClient(MousePosition));
            if (hi.InRow || hi.InRowCell)
                if (bsSipNo.Current != null)
                {
                    try
                    {
                        PONr = ((DataRowView)bsSipNo.Current).Row["SipNo"].ToString();

                        SipAcanMarkaId = 0;
                        int.TryParse(((DataRowView)bsSipNo.Current).Row["BrandId"].ToString(), out SipAcanMarkaId);

                        this.DialogResult = DialogResult.OK;

                    }
                    catch (Exception Ex)
                    {
                        throw (Ex);
                    }
                }
        }

    }
}